package com.zl.learning.http;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;

import java.io.*;
import java.util.*;

public class TestHttpClient {

    public static final String BASE_URL = "http://172.31.4.241";
    public static final String SEARCH_URL = BASE_URL + ":8082/druid/v2?pretty";
    public static final String SUBMIT_URL = BASE_URL + ":8081/druid/indexer/v1/supervisor";
    public static final String SQL_URL = BASE_URL + ":8082/druid/v2/sql/";
    public static final String DEL_URL = BASE_URL + ":8081/druid/indexer/v1/task";
    public static final String DROP_URL = BASE_URL + ":8081/druid/coordinator/v1/datasources/deletion-tutorial/markUnused";


    private Gson gson = new Gson();

    @Test
    public void test1() throws IOException {
        String param = gson.fromJson(new FileReader("param/topN.json"), JsonObject.class).toString();
        String res = doPost(param, SEARCH_URL);
        System.out.println(res);
    }

    @Test
    public void test2() throws FileNotFoundException {
        String param = gson.fromJson(new FileReader("param/timeseries.json"), JsonObject.class).toString();
        String res = doPost(param, SEARCH_URL);
        System.out.println(res);
    }

    @Test
    public void test3() {
        List<String> users = new ArrayList<>(Arrays.asList("111", "222", "333", "444"));
//        users.forEach(r->{
//            users.remove(r);
//        });
        System.out.println(users.size());
        for (Iterator<String> it = users.iterator(); it.hasNext(); ) {
            String u = it.next();
            System.out.println(u);
            it.remove();
        }
        System.out.println(users.size());
    }

    @Test
    public void test4() {
        Map<String, String> param = new HashMap<>();
        String sql = "select ID,PLATENUMBER from vehicle where ACCESSTIME > '2019-08-09 17:25:00' and ACCESSTIME < '2019-08-09 17:30:00' group by ID,PLATENUMBER ";
        param.put("query", sql);
        String res = doPost(gson.toJson(param), SQL_URL);
        System.out.println(res);
    }

    @Test
    public void test5() {
        Map<String, String> param = new HashMap<>();
        String sql = "select * from wikipedia  limit 10  ";
        param.put("query", sql);
        String res = doPost(gson.toJson(param), SQL_URL);
        System.out.println(res);
    }

    @Test
    public void testDropData() throws FileNotFoundException {
//        String param = gson.fromJson(new FileReader("param/deletion-kill.json"), JsonObject.class).toString();
        String res = doPost("{ \"interval\" : \"2019-01-09T00:00:00.000Z/2019-11-27T00:00:00.000Z\" }", DROP_URL.replace("deletion-tutorial", "hotspot"));
//        String res = doPost("{\n" +
//                "  \"segmentIds\":\n" +
//                "  [\n" +
//                "    \"vehicle_2019-08-09T00:00:00.000Z_2019-08-10T00:00:00.000Z_2019-08-09T02:08:24.705Z\",\n" +
//                "    \"vehicle_2019-08-09T00:00:00.000Z_2019-08-10T00:00:00.000Z_2019-08-09T02:08:24.705Z_1\"\n" +
//                "  ]\n" +
//                "}", DROP_URL.replace("deletion-tutorial","vehicle"));
        System.out.println(res);
    }

    @Test
    public void testDelData() throws FileNotFoundException {
//        String param = gson.fromJson(new FileReader("param/deletion-kill.json"), JsonObject.class).toString();
        String res = doPost("{\"type\": \"kill\", \"dataSource\": \"hotspot\", \"interval\" : \"1970-08-22/2019-11-27\"}", DEL_URL);
        System.out.println(res);
    }

    @Test
    public void testSubmitVehicleIndex() throws FileNotFoundException {
        String param = gson.fromJson(new FileReader("param/kafka-hotspot-index.json"), JsonObject.class).toString();
        String res = doPost(param, SUBMIT_URL);
        System.out.println(res);
    }

    @Test
    public void test6() {
        String[] str = ("0|1|2|3|4|5|6|||"+"|0").split("\\|");
        System.out.println(Arrays.toString(str));
    }

    private String doPost(String param, String url) {
        try (CloseableHttpClient httpclient = HttpClients.createDefault();) {
            System.out.println(url);
            HttpPost post = new HttpPost(url);
            post.setEntity(new StringEntity(param, ContentType.APPLICATION_JSON));
            HttpResponse response = httpclient.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                InputStream in = entity.getContent();
                ByteArrayOutputStream result = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) != -1) {
                    result.write(buffer, 0, length);
                }
                return result.toString("UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
